package com.kws.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kws.common.core.domain.R;
import com.kws.system.domain.info.QueryDictDataInfo;
import com.kws.system.domain.vo.CreateDictDataRq;
import com.kws.system.domain.vo.QueryDictDataRq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.kws.common.core.utils.StringUtils;
import com.kws.common.core.utils.poi.ExcelUtil;
import com.kws.common.log.annotation.Log;
import com.kws.common.log.enums.BusinessType;
import com.kws.common.security.annotation.RequiresPermissions;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.api.domain.SysDictData;
import com.kws.system.service.ISysDictDataService;
import com.kws.system.service.ISysDictTypeService;

/**
 * 数据字典信息
 * 
 * @author zhangyh
 */
@RestController
@Api(tags = "数据字段管理模块")
@RequestMapping("/dict/data")
public class SysDictDataController
{
    @Autowired
    private ISysDictDataService dictDataService;
    
    @Autowired
    private ISysDictTypeService dictTypeService;

//    @RequiresPermissions("system:dict:list")
    @ApiOperation("查询数据字典列表")
    @PostMapping("/list")
    public R<Page<SysDictData>> list(@RequestBody QueryDictDataRq rq)
    {
        Page<SysDictData> pageInfo = dictDataService.selectDictDataList(rq);
        return R.ok(pageInfo);
    }

//    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
////    @RequiresPermissions("system:dict:export")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysDictData dictData)
//    {
//        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        util.exportExcel(response, list, "字典数据");
//    }

    /**
     * 查询字典数据详细
     */
//    @RequiresPermissions("system:dict:query")
    @ApiOperation("根据字典编码查询详情")
    @GetMapping(value = "/{dictCode}")
    public R<QueryDictDataInfo> getInfo(@PathVariable Long dictCode) {
        return R.ok(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    @ApiOperation("根据字典类型查询字典数据信息")
    public R dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (CollectionUtil.isEmpty(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return R.ok(data);
    }

    /**
     * 新增字典类型
     */
//    @RequiresPermissions("system:dict:add")
    @ApiOperation("新增数据字典")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody CreateDictDataRq rq)
    {
        return dictDataService.createDictData(rq);
    }

    /**
     * 修改保存字典类型
     */
//    @RequiresPermissions("system:dict:edit")
    @ApiOperation("修改数据字典信息")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
//    @RequiresPermissions("system:dict:remove")
    @ApiOperation("根据ID批量删除数据字典信息")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return R.ok();
    }

    /**
     * 平台状态修改
     */
    @ApiOperation("平台状态修改")
    @GetMapping("/platform/{status}")
    public R statusUpdate(@PathVariable(value = "status") String status)
    {
        SysDictData dictData = new SysDictData();
        dictData.setDictCode(11111111111L);
        dictData.setDictValue(status);
        dictDataService.updateDictData(dictData);
        return R.ok();
    }
}
